<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 200px;
            height: 200px;
            position: relative;
            left: 0;
            background-color: pink;
        }

        span {
            position: relative;
            left: 0;
            top: 20px;
            display: block;
            height: 150px;
            width: 150px;
            background-color: red;

        }
    </style>
</head>

<body>
    <button class="haha">点击</button>
   
    <div></div>
    <span></span>
    <script>

        function animate(obj, target,callback) {
            clearInterval(obj.timer);

            obj.timer = setInterval(function () {
                let step =(target - obj.offsetLeft) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step) ; 
                if (obj.offsetLeft == target) {
                    clearInterval(obj.timer);
                    if(callback){
                        callback();
                    }
                }
                obj.style.left = obj.offsetLeft + step + 'px';
            }, 15)
        }
        let div = document.querySelector('div');
        animate(div, 300);
        let span = document.querySelector('span');
        let btn = document.querySelector('button');
        btn.addEventListener('click', function () {
            animate(span, 2000,function(){
                // btn.style.backgroundColor = 'green';
        alert("kkkk");
            });
        })

    </script>
</body>

</html>